﻿using CastingWellEquipmentGUI.Config;
using CastingWellEquipmentGUI.Dao;
using CastingWellEquipmentGUI.Model;
using CastingWellEquipmentGUI.Utils;
using CastingWellEquipmentGUI.ViewModel.Common.Command;
using CastingWellEquipmentGUI.ViewModel.Common.LogTool;
using Confluent.Kafka;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CastingWellEquipmentGUI.ViewModel.Modal.DrainageSystem
{
    public class OnOrOffAndZhuOrFuPanelWindowViewModel : ViewModelBase
    {
        Control currentControl;
        /// <summary>
        /// 关闭当前窗体命令
        /// </summary>
        public DelegateCommand CloseAppCommand { get; set; }
        public DelegateCommand OperationCommand { get; set; }


        //等待Kafaka改变标签值结果的消息
        private IConsumer<string, object> KafkaConsumerOfChangeValue = KafkaHelper.GetConsumerChangeValue();
        /// <summary>
        /// 回调消息令牌名称
        /// </summary>
        private string CallbackFuncation;
        /// <summary>
        /// 提供给调用者用来设置回调消息名称
        /// </summary>
        /// <param name="CallbackFuncationName">回调消息令牌名称</param>
        public void SetCallbackFuncationName(string CallbackFuncationName)
        {
            CallbackFuncation = CallbackFuncationName;
        }

        public OnOrOffAndZhuOrFuPanelWindowViewModel()
        {
            CloseAppCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(CloseAppHandler)
            };
            OperationCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(OperationHandler)
            };
        }

        /// <summary>
        /// 退出程序命令处理程序
        /// </summary>
        /// <param name="obj">参数</param>
        private void CloseAppHandler(object obj)
        {
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "OnOrOffAndZhuOrFuPanel"));
        }
        private void OperationHandler(object obj)
        {
            //发送结果
            Messenger.Default.Send<string>(obj.ToString(), CallbackFuncation);
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "OnOrOffAndZhuOrFuPanel"));
        }
        /// <summary>
        /// 根据窗体名称和控件名称加载当前控制的标签
        /// </summary>
        /// <param name="framName"></param>
        /// <param name="controlName"></param>
        public void LoadTagInfoByFramNameAndControlName(object framName, object controlName)
        {
            for (int i = 0; i < LogTool.CurrentUser.Roles.Count; i++)
            {
                for (int j = 0; j < LogTool.CurrentUser.Roles[i].Permissions.Count; j++)
                {
                    if (LogTool.CurrentUser.Roles[i].Permissions[j].PForm != null &&
                        LogTool.CurrentUser.Roles[i].Permissions[j].PControl != null)
                    {
                        if (LogTool.CurrentUser.Roles[i].Permissions[j].PForm.FormName == framName.ToString() &&
                                               LogTool.CurrentUser.Roles[i].Permissions[j].PControl.ControlName == controlName.ToString())
                        {
                            currentControl = LogTool.CurrentUser.Roles[i].Permissions[j].PControl;
                            return;
                        }
                    }

                }
            }
        }

    }
}
